﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Xml;

namespace OMSWeb.API_Demo
{
    public partial class taobaoitemsget : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void CreatTable(DataTable dt)
        {
            //dt = new DataTable("ITEM");

            dt.Columns.Add("num_iid", typeof(string));
            dt.Columns.Add("title", typeof(string));

            dt.Columns.Add("delist_time", typeof(DateTime));
            dt.Columns.Add("nick", typeof(string));

            dt.Columns.Add("pic_url", typeof(string));
            dt.Columns.Add("price", typeof(string));

            dt.Columns.Add("type", typeof(string));
            dt.Columns.Add("cid", typeof(string));
            dt.Columns.Add("post_fee", typeof(string));

        }

        private void AddXmlNodesToTable(XmlNodeList nodes, DataTable dt)
        {
            DataRow dr = null;

            foreach (XmlNode node in nodes)
            {
                dr = dt.NewRow();
                dr["num_iid"] = node.SelectSingleNode("num_iid").InnerText;
                dr["title"] = node.SelectSingleNode("title").InnerText;

                dr["delist_time"] = node.SelectSingleNode("delist_time").InnerText;
                dr["nick"] = node.SelectSingleNode("nick").InnerText;
                if (node.SelectSingleNode("pic_url") != null)
                {
                    dr["pic_url"] = node.SelectSingleNode("pic_url").InnerText;
                }

                dr["price"] = node.SelectSingleNode("price").InnerText;
                dr["type"] = node.SelectSingleNode("type").InnerText;
                dr["cid"] = node.SelectSingleNode("cid").InnerText;
                //dr["post_fee"] = "";
                dt.Rows.Add(dr);

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            CreatTable(dt);


            string appkey = "test";

            string secret = "test";

            ////string session = "terry0057";//他用类型 且是获取私有数据的需要传递 sessionkey

            ////http://container.api.tbsandbox.com/container?appkey=12564689&encode=utf-8 

            IDictionary<string, string> param = new Dictionary<string, string>();


            //param.Add("fields", "nick,location.state,location.city");
            //param.Add("nick", "terry0057");

            ////Util.Post 集成了 系统参数 和 计算 sign的方法

            //Response.Write("返回结果：" + Util2.Post("http://gw.api.tbsandbox.com/router/rest", appkey, secret, "taobao.user.get", param));
            param.Add("fields", "num_iid,title,nick,pic_url,cid,price,type,delist_time,post_fee");
            param.Add("q", txtValue.Text);
            //nicks 
            param.Add("page_no", "1");
            param.Add("page_size", "40");

            //param.Add("nick", );
            XmlDocument xmlDoc = PostTool.Post("http://gw.api.tbsandbox.com/router/rest", appkey, secret, "taobao.items.get", param);


            XmlNodeList mylist = xmlDoc.SelectNodes("items_get_response/items/item");
            //int num = int.Parse(xmlDoc.SelectSingleNode("/rsp/totalResults").InnerText);
            try
            {
                AddXmlNodesToTable(mylist, dt);

                this.GridView1.DataSource = dt;
                this.GridView1.DataBind();
            }
            catch
            {
            }
        }
    }
}
